<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Config;
use app\common\model\StaffClock;
use app\common\model\StaffView;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 技师接口
 */
class Staff extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Staff();
    }

    //技师列表
    public function list()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit',LIMIT);
        $tag_id = $this->request->get('tag_id');
        $condition = $this->request->get('condition',0);
        $check_state = $this->request->get('check_state');
        $is_share = $this->request->get('is_share');
        $hot = $this->request->get('hot');
        $city = $this->request->get('city');

        $age = $this->request->get('age');
        $height = $this->request->get('height');
//        $where['is_vip'] = 0;
        if(empty($hot)) {
            $where['state'] =['in','1,2'];
        }else  {
            $where['state'] = '2';
        }


        if(!empty($tag_id)) $where['tag_id'] = $tag_id;
        if(!empty($check_state)) $where['check_state'] = $check_state;
        if(!empty($is_share)) $where['is_share'] = 1;

        if(!empty($age)) {
            $age_where = [];
            switch ($age) {
                case '1':
                    $age_where = ['between','18,21'];
                    break;
                case '2':
                    $age_where = ['between','21,24'];
                    break;
                case '3':
                    $age_where = ['between','24,28'];
                    break;
                case '4':
                    $age_where = ['>=','28'];
                    break;
            }
            $where['age'] = $age_where;
        }

        if(!empty($height)) {
            $height_where = [];
            switch ($height) {
                case '1':
                    $height_where = ['between','150,155'];
                    break;
                case '2':
                    $height_where = ['between','155,160'];
                    break;
                case '3':
                    $height_where = ['between','160,165'];
                    break;
                case '4':
                    $height_where = ['>=','165'];
                    break;
            }

            $where['height'] = $height_where;
        }
        $order = "";
        switch ($condition) {
            case '1':
                $order = "weigh desc";
                break;
            case '2':
                $order = "price desc";
                break;
            case '3':

                break;
        }
        $where['city'] = $city;
        $list =  $this->model->with(['tag'])->where($where)->order($order)->paginate($limit,false,['page' => $page]);

        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success('返回成功',$result);
    }

    public function viplist()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit',LIMIT);
        $tag_id = $this->request->get('tag_id');
        $state = $this->request->get('state',1);
        $check_state = $this->request->get('check_state');
        $is_share = $this->request->get('is_share');
        $city = $this->request->get('city');

        $where = ['is_vip' => 1];
        $where['city'] = $city;
        $order = "weigh desc";
        if(!empty($tag_id)) $where['tag_id'] = $tag_id;
        if(!empty($check_state)) $where['check_state'] = $check_state;
        if(!empty($is_share)) $where['is_share'] = 1;
        switch ($state) {
            case '1':
                $order = "createtime desc";
                break;
            case '2':
                $order = "price desc";
                break;
            case '3':

                break;
        }
        $list =  $this->model->with(['tag'])->where($where)->order($order)->paginate($limit,false,['page' => $page]);

        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success('返回成功',$result);
    }

    //技师入驻
    public function apply()
    {
        $data = $this->request->post();

        Db::startTrans();
        try {
            $data['user_id'] = $this->auth->id;
            $staff = $this->model->where(['user_id' => $this->auth->id])->find();
            if($staff) {
                $data['check_state'] = 0;
                \app\common\model\Staff::update($data);
            }else {
//                $user = \app\common\model\User::get($this->auth->id);
//                $user->is_staff = 1;
//                $user->save();
                \app\common\model\Staff::create($data);
            }
            Db::commit();
        }catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('成功');
    }
    //技师详情
    public function detail()
    {
        $user = \app\common\model\User::get($this->auth->id);

        $id = $this->request->get('id');
        $info =  $this->model->with([ 'tag'])->where(['id' => $id])->find();

        if($this->auth->level != 5) {
            if($this->auth->view <= 0) {
                $this->error('免费查看额度已经用完，请立即办理会员','',4);
            };
            //未查看
            $view = StaffView::get(['user_id' => $this->auth->id, 'staff_id' => $id]);
            if(empty($view)) {
                StaffView::create(['user_id' => $this->auth->id, 'staff_id' => $id]);
                //扣除次数
                $user->view = $user->view - 1;
                $user->save();
            }
        }

        //如果购买了
        $staff = StaffClock::get(['user_id' => $this->auth->id, 'staff_id' => $id]);
        $info->is_clock = 0;
        if($staff) {
            $info->hidden(['updatetime','deletetime']);
            $info->is_clock = 1;
        }else {
            $info->hidden(['mobile', 'address', 'character','updatetime','deletetime']);
        }
        $info->getRelation('tag')->hidden(['createtime','updatetime','deletetime']);
        $this->success('返回成功',$info);
    }

    //查找技师
    public function info($no = null)
    {
        $this->success('成功',  \app\common\model\Staff::get(['no' => $no]));
    }

    //技师审核状态
    public function checkstate()
    {
        $info = \app\common\model\Staff::get(['user_id' => $this->auth->id]);
        if($info) {
            $this->success('成功', $info);
        }else {
            $this->error('失败');
        }
    }

    //解锁技师
    public function clock()
    {
        $staff_id = $this->request->get('staff_id');
        $staff =  $this->model->where(['id' => $staff_id])->find();
        if(empty($staff)) $this->error('参数错误');
        $money = $staff->price;
        $user = $this->auth->getUser();
        //判断用户有没有查看次数
//        if($user->view == 0) {
//            $this->error('免费查看额度已经用完，请立即办理会员','',4);
//        }
        //判断用户有没有余额
        if($user->money < $money) {
            $this->error('当前余额不足，请立即充值','',3);
        }

        $order = StaffClock::get([ 'staff_id' => $staff_id, 'user_id' => $this->auth->id]);

        if(!empty($order)) $this->error('当前技师已解锁');

        Db::startTrans();
        try {
            $model = new StaffClock();
            $model->save([
                'staff_id' => $staff_id,
                'user_id' => $this->auth->id,
                'money' => $money
            ]);
            $to_user = \app\common\model\User::get($staff->user_id);

            $payamount = $money;
            $memo1 = "你解锁了{$to_user->nickname}的联系方式";
            $memo2 = "{$user->nickname}解锁你的联系方式";

            \app\common\model\Message::setMessage($this->auth->id,$memo1,'/pages/settle/detail?id='.$staff_id);
            //消息
            \app\common\model\Message::setMessage($staff->user_id,$memo2,'/pages/settle/detail?id='.$staff_id);
            //分成
            \app\common\model\User::money(-$payamount,$this->auth->id,$memo1);

            $switch = Config::get(['group' => 'example', 'name' => 'clock_switch'])->value;
            $ratio = Config::get(['group' => 'example', 'name' => 'clock_ratio'])->value;

            if(!empty($user->p_id) && !empty($switch)) {
                $newpayamount = $payamount * ($ratio / 100);
                \app\common\model\UserIncome::money($newpayamount,$user->p_id,$user->nickname.'解锁花名册');
            }

            Db::commit();
        }catch (ValidateException|PDOException|\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success(__('解锁成功'));
    }

    //我的花名册
    public function mystafflist()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit',LIMIT);
        $where = [
            'fa_staff_clock.user_id' => $this->auth->id,
        ];
        $model = new StaffClock();
        $list =  $model->where($where)->with([ 'staff'])->paginate($limit,false,['page' => $page]);
        foreach ($list as $row) {
            $row->getRelation('staff')->hidden(['mobile', 'height', 'weight', 'age', 'figure', 'city', 'no', 'address', 'character','updatetime','deletetime']);
        }
        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success('返回成功',$result);
    }
}